#include <iostream>
#include "card_compartor.h"
using namespace dymy;
using namespace dymy::card;
using namespace std;

std::vector<card_t> split(std::string ss, std::string s);
std::ostream &operator<<(std::ostream &os, const compartor::CardsClassify &item);
std::ostream &operator<<(std::ostream &os, const std::vector<card_t> &cards);
std::ostream &operator<<(std::ostream &os, const std::vector<std::vector<card_t>> &cards);

int main(int, char **)
{

    while (1)
    {
        cout << "input left cards:" << endl;
        string left_cards_str;
        getline(cin, left_cards_str);
        dymy::compartor::CardsClassify left;
        left.compute_cards(split(left_cards_str, " "));
        cout << "input right cards:" << endl;
        string right_cards_str;
        getline(cin, right_cards_str);
        dymy::compartor::CardsClassify right;
        right.compute_cards(split(right_cards_str, " "));
        cout << '[' << left << ']' << "<=" << '[' << right << ']' << "->" << (left <= right) << endl
             << endl;
    }
}

std::vector<card_t> split(std::string ss, std::string s)
{
    std::vector<card_t> cards;
    const int s_l = s.length();
    int start_index = 0;

    int end_index;
    while ((end_index = ss.find(s, start_index)) >= 0)
    {
        std::string str = ss.substr(start_index, end_index - start_index);
        if (str.length() > 0)
        {
            cards.push_back(static_cast<card_t>(std::stoi(str)));
        }
        start_index = end_index + s_l;
    }
    if (start_index < ss.length())
    {
        std::string str = ss.substr(start_index, end_index - start_index);
        if (str.length() > 0)
        {
            cards.push_back(static_cast<card_t>(std::stoi(str)));
        }
    }
    return cards;
}
std::ostream &operator<<(std::ostream &os, const dymy::card_t &c)
{
    return os << static_cast<int>(c) << "(" << static_cast<int>(dymy::card::decode_card_num(c)) << ")";
}
std::ostream &operator<<(std::ostream &os, const compartor::CardsClassify &item)
{
    if (item.is_invalid())
    {
        os << "Invalid!!!";
    }
    else
    {
        os << item.c_continuous << item.c4 << item.c3 << item.c2 << item.c1 << item.c_king;
    }
    return os;
}
std::ostream &operator<<(std::ostream &os, const std::vector<card_t> &cards)
{
    for (auto begin = cards.cbegin(); begin != cards.cend(); ++begin)
    {
        os << *begin << ' ';
    }
    return os;
}

std::ostream &operator<<(std::ostream &os, const std::vector<std::vector<card_t>> &cards)
{
    for (auto begin = cards.cbegin(); begin != cards.cend(); ++begin)
    {
        os << *begin;
    }
    return os;
}
